package com.dianmi.seckill.common.log;

import com.dianmi.seckill.common.log.impl.DefaultLogCutter;
import com.dianmi.seckill.common.log.impl.LogContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xieqiaofu
 * @date 2020/11/17
 */
@Aspect
@Component
public class MethodLogAspect {

    @Autowired
    private LogConfiguration logConfiguration;

    @Around(value = "@annotation(MethodLog)")
    public Object aroundMethod(ProceedingJoinPoint point) throws Throwable {
        LogContext.setLogConfiguration(logConfiguration);
        MethodSignature signature = (MethodSignature)point.getSignature();
        LogCutter logCutter = new DefaultLogCutter(signature);
        logCutter.beforeMethod(point.getArgs());
        Object returnObj = null;
//        try {
            returnObj = point.proceed();
//        } catch (Throwable e) {
//            logCutter.throwMethod(e);
//            throw e;
//        }
        logCutter.afterMethod(returnObj);
        return returnObj;
    }
}
